# @Time     : 2019/4/15 14:45
# -*- coding : utf-8 -*-
# @Author   : GKL

import re
import base64
from io import BytesIO
from lxml import etree
import requests
from fontTools.ttLib import TTFont


class Spider(object):
    def __init__(self):
        self.url = "https://fz.58.com/searchjob/?key=%E4%BA%92%E8%81%94%E7%BD%91"
        self.headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 \
            (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
        }

    def get_base64_data(self):
        """
        获取网页base64编码
        :return:
        """
        resp = requests.get(self.url, headers=self.headers).text
        base64_str = re.findall(r"base64,(.*?)\)", resp)[0]  # base64 字符串
        return base64_str, resp

    def make_xml(self, base64_str):
        """
        xml文件生成
        :param base64_str:
        :return:
        """
        data = base64.decodebytes(base64_str.encode())
        font = TTFont(BytesIO(data))
        font.saveXML("result.xml")

    def parse_xml(self, filename):
        """
        解析xml文件 获取name属性和轨迹关系 编码和name熟悉关系
        :param filename:
        :return:
        """
        tree = etree.parse(filename)
        root = tree.getroot()
        items = {}  # name属性和轨迹之间的关系
        for i in root.xpath("//TTGlyph"):
            value = re.findall(r'(xMin=".*?" yMin=".*?" xMax=".*?" yMax=".*?")', etree.tostring(i).decode(), re.S)
            if value:
                items[i.get("name")] = value[0]

        unicode_items = {}  # 编码和name属性的关系
        for i in root.xpath("//map"):
            unicode_items[i.get("code")] = i.get("name")
        return items, unicode_items

    def parse_data(self, resp):
        """
        获取网页所有用户信息
        :param resp:
        :return:
        """
        user_list = re.findall(r'<span class="infocardName fl stonefont resumeName">(.*?)</span>', resp)
        print(user_list)

    def parse_unicode(self, items, unicode_items, items_dict, resp):
        """
        :param items: name属性和轨迹之间的关系
        :param unicode_items: 编码和name属性的关系
        :param items_dict: 轨迹和汉字
        :param resp: 网页源码
        :return:
        """
        unicode_list = re.findall(r"&#(.*?);", resp)  # 所有Unicode编码
        positions = []  # 所有汉字
        for i in unicode_list:
            unicode_name = unicode_items.get("0{}".format(i))  # 编码对应name属性
            track = items.get(unicode_name)  # name属性对应的轨迹
            track_result = items_dict.get(track)  # 轨迹对应汉字
            positions.append(track_result)
        self.replace_data(unicode_list, positions, resp)

    def replace_data(self, unicode_list, positions, resp):
        """
        :param unicode_list: 所有编码
        :param positions: 所有汉字
        :param resp: 网页源码
        :return:
        """
        items_unicode = {}  # 编码和汉字
        for i, n in zip(unicode_list, positions):
            items_unicode[i] = n

        for key in items_unicode.keys():
            if items_unicode.get(key):
                resp = resp.replace("&#{};".format(key), items_unicode.get(key))
        self.parse_data(resp)

    def run(self):
        items_dict = {
            'xMin="0" yMin="-271" xMax="1940" yMax="1673"': "杨",
            'xMin="0" yMin="-145" xMax="2012" yMax="1709"': "生",
            'xMin="0" yMin="-273" xMax="1990" yMax="1705"': "女",
            'xMin="0" yMin="-117" xMax="2000" yMax="1699"': "士",
            'xMin="0" yMin="-147" xMax="1982" yMax="1521"': "王",
            'xMin="0" yMin="-255" xMax="2022" yMax="1697"': "李",
            'xMin="0" yMin="-259" xMax="2020" yMax="1691"': "张",
        }
        base64_str, resp = self.get_base64_data()
        self.make_xml(base64_str)
        items, unicode_items = self.parse_xml("result.xml")
        self.parse_unicode(items, unicode_items, items_dict, resp)


if __name__ == '__main__':
    s = Spider()
    s.run()
